package com.android.gufengmall.adapter;

import android.content.Context;
import android.nfc.Tag;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.android.gufengmall.R;
import com.android.gufengmall.bean.DiscussBean;
import com.android.gufengmall.utils.BitMapConversionUtils;
import com.bumptech.glide.Glide;

import org.jetbrains.annotations.NotNull;

import java.util.List;

public class ForumCommentAdapter extends RecyclerView.Adapter<ForumCommentAdapter.ViewHolder> {

    private static final String TAG = "ForumCommentAdapter";

    private Context mContext;
    private List<DiscussBean> mDiscussBeans;

    public ForumCommentAdapter(Context context, List<DiscussBean> discussBeans) {
        mContext = context;
        mDiscussBeans = discussBeans;
    }

    @NotNull
    @Override
    public ForumCommentAdapter.ViewHolder onCreateViewHolder(@NonNull @NotNull ViewGroup viewGroup, int i) {
        return new ViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item_my_forum_comment,viewGroup,false));
    }

    @Override
    public void onBindViewHolder(@NonNull @NotNull ForumCommentAdapter.ViewHolder viewHolder, int i) {
        Log.d(TAG,"mDiscussBeans-->" + mDiscussBeans);
        if (mDiscussBeans.size() >= 1) {
            DiscussBean discussBean = mDiscussBeans.get(i);
            viewHolder.mTvInfo.setText(discussBean.getInfo());
            viewHolder.mTvUserName.setText(discussBean.getUserName());
            viewHolder.mTvData.setText(discussBean.getDate());
            Glide.with(mContext).load(BitMapConversionUtils.stringToBitmap(discussBean.getUserPhoto()))
                    .dontAnimate().error(R.drawable.icon_photo).fitCenter().into(viewHolder.mImgPhoto);
        }
    }

    @Override
    public int getItemCount() {
        Log.d(TAG,"getItemCount-size->" + mDiscussBeans.size());
        if (mDiscussBeans != null) {
            return mDiscussBeans.size();
        } else {
            return 0;
        }
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        private final TextView mTvInfo;
        private final ImageView mImgPhoto;
        private final TextView mTvUserName;
        private final TextView mTvData;

        public ViewHolder(@NonNull @NotNull View itemView) {
            super(itemView);

            mTvInfo = itemView.findViewById(R.id.tv_info);
            mImgPhoto = itemView.findViewById(R.id.img_photo);
            mTvUserName = itemView.findViewById(R.id.tv_userName);
            mTvData = itemView.findViewById(R.id.tv_date);
        }
    }
}
